(function (jQuery) {
    // $(function () {
    //     let oUser=JSON.parse(sessionStorage.getItem("sasauserinfo"));
    //
    //     $("#txt").html("欢迎:"+oUser.u_name);
    //     $("Login_header>p").css("display","none");
    //
    //     $("#close").on("click",function(){
    //         sessionStorage.removeItem("sasauserinfo");
    //         window.location="login.html";
    //         $("Login_header>p").css("display","block");
    //         $(this).css("display","none")
    //     })
    // })

    $(function () {
        $.idcode.setCode();//加载生成验证码方法
        //调取用户名的cookie
        $(document).ready(function () {
            if ($.cookie("rmbUser") == "true") {
                $("[name=checkbox]").attr("checked", true);
                $("[name=name]").val($.cookie("username"));
                $("[name=pwd]").val($.cookie("password"));
            }
        });

        $("#myform").on("submit",function () {
             //验证码
            var status=$.idcode.validateCode();//调用返回值,返回值结果是true或者false
            if(!status){
                alert("验证码错误");
                return;
            }
            $.ajax({
                url:"../../server/login.php",
                // data:$("form").serialize(),  需前后台对接好 才可好用
                data:{uname:$("[name=name]").val(),upwd:$("[name=pwd]").val()},
               // data:"uname="+this.uname.value+"&upwd="+this.upwd.value,
                type:"post",
                dataType : "json"
            }).then(function (res) {
                if(res.status==1){


                    //添加到购物车中
                    console.log($.cookie("carts"));
                    let CookieList=JSON.parse($.cookie("carts")||'[]');
                    console.log(CookieList);
                    var arr=[];
                    CookieList.forEach((ele,index)=>{
                        var ele = CookieList[index];
                        ele.u_id=res.data.u_id;
                        ele.img=ele.img[0];
                        console.log(ele);
                        let p=$.ajax({
                            url:"../../server/addCart.php",
                            type:"post",
                            data:ele
                        });
                        arr.push(p.promise());
                    })

                    // $.cookie("carts","",{expires:-1});
                    //all 并发走完了,才会指向then
                    Promise.all(arr).then(function (res) {
                        //执行完成后,清空cookie

                        $.cookie("carts","",{expires:-1});

                    })

                    if ($("[name=checkbox]").prop('checked')){
                        var str_username = $("[name=name]").val();
                        var str_password =  $("[name=pwd]").val();
                        //把当前的用户名信息存储到cookie
                        $.cookie("rmbUser", "true", { expires: 7 }); //存储一个带7天期限的cookie
                        $.cookie("username", str_username, { expires: 7 });
                        $.cookie("password", str_password, { expires: 7 });
                        console.log(res.data);
                        //再把当前的用户名信息存储到sessionStorage
                        sessionStorage.setItem("sasauserinfo",JSON.stringify(res.data));
                        alert("登录成功");


                        window.location = "index1.html";
                    }else {
                        $.cookie("rmbUser", "false", { expire: -1 });
                        $.cookie("username", "", { expires: -1 });
                        $.cookie("password", "", { expires: -1 });

                        confirm("是否7天免登录")
                        window.location = "index1.html";
                    }


                }else{
                    alert(res.msg);
                }
            })
            return false;
        })
    })
})(jQuery)